<?php

namespace App\Http\Controllers\Home\Address;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use session;
use DB;
class AddressController extends Controller
{
    //收货地址
    public function address()
    {
      
    	//获取uid
    	$uid = session('master')['id'];

    	//查询数据库
    	$data = DB::table('address') -> where('uid',$uid) -> get();

    	if($data)
    	{
    		return view('home.address.address',['data' => $data]);

    	}else
    	{
    		return view('home.address.address', ['data' => '0']);
    	}
    	
    	
        
    }	

    public function add(Request $request)
    {
    	// 此时验证出错时提示的英文,要把英文变成中文
	    $this->validate($request, [
	        'province' => 'required',
	        'city' => 'required',
	        'area' => 'required',
	        'address' => 'required',
	        'acc_name' => 'required',
	        'acc_phone' => 'required'
	    ],[
        	'province.required' => '省不能为空',
            'city.required' => '市不能为空',
            'area.required' => '地区不能为空',
            'address.required' => '详细地址不能为空',
            'acc_name.required' => '收货人不能为空',
            'acc_phone.required' => '收货人电话不能为空'
	    ]);

    	//获取用于的id
	    $uid = session('master')['id'];

	    $num = DB::table('address') -> where('uid', $uid) -> count();

	    if($num >= 5)
	    {
	    	return redirect('/home/address') -> with(['info' => '只能添加5条收货地址']);
	    }

	    //出去多余的字段
	    $data = $request -> except('_token');

	    //把uid压入数组
	    $data['uid'] = $uid;

	    //写入数据库
	    $res = DB::table('address') -> insertGetId($data);

	    if($res)
	    {
	    	return redirect('/home/address');
	    }
	    return back() -> width(['info' => '添加失败']);
    }





    public function status($id)
    {
    	
    	//获取用户id
    	$uid = session('master')['id'];

		$res = DB::table('address') -> where('uid', $uid) -> update(['is_default' => '1']);

    	$res = DB::table('address') -> where('uid', $uid) -> update(['is_default' => '0']);
    	
    	if($res)
    	{
    		$res = DB::table('address') -> where('id', $id) -> update(['is_default' => '1']);
    		if($res)
    		{	
    			
    			return redirect('/home/address');
    		}else
    		{
    			return back() -> with(['info' => '失败']) ;
    		}
    		
    	}else
    	{
    		return back() -> with(['info' => '失败']);
    	}
    }


   	public function edit(Request $request)
   	{
   		//去除多余字段
   		$data = $request -> except('_token');
   		
   		//获取uid
   		$uid = session('master')['id'];

   		//取出修改信息
   		$id = $request -> edit_id;
   		$acc_name = $request -> acc_name;
   		$acc_phone = $request -> acc_phone;
   		$province = $request -> province;
   		$city = $request -> city;
   		$area = $request -> area;
   		$address = $request -> address;

   		//修改数据库
   		$res = DB::table('address') -> where('id', $id) -> update(['acc_name' => $acc_name, 'acc_phone' => $acc_phone, 'province' => $province, 'city' => $city, 'area' => $area, 'address' => $address]);

   		if($res)
   		{
   			return redirect('/home/address');
   		}else
   		{
   			return back() -> with(['info' => '修改失败']);
   		}
   	}

   	public function delete($id)
   	{
   		//删除数据
   		$res = DB::table('address') -> where('id', $id) -> delete();
   		if($res)
   		{
   			return redirect('/home/address');
   		}else
   		{
   			return back() -> with(['info' => '删除失败']);
   		}
   	}
}
